Skip to content

Method: static {...}

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.rdf4j.query;
19:
20: import cz.cvut.kbss.ontodriver.ResultSet;
21: import cz.cvut.kbss.ontodriver.Statement;
22: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
23:
24: import java.util.NoSuchElementException;
25:
26: abstract class AbstractResultSet implements ResultSet {
27:
28: private final Statement statement;
29:
30: private int index;
31: private boolean open;
32:
33: AbstractResultSet(Statement statement) {
34: assert statement != null;
35:
36: this.statement = statement;
37: this.index = -1;
38: this.open = true;
39: }
40:
41: @Override
42: public boolean isOpen() {
43: return open;
44: }
45:
46: @Override
47: public int getRowIndex() {
48: ensureOpen();
49: return index;
50: }
51:
52: @Override
53: public Statement getStatement() {
54: ensureOpen();
55: return statement;
56: }
57:
58: @Override
59: public boolean isFirst() {
60: ensureOpen();
61: return index == 0;
62: }
63:
64: void ensureOpen() {
65: if (!open) {
66: throw new IllegalStateException("The result set is closed!");
67: }
68: }
69:
70: @Override
71: public void close() throws OntoDriverException {
72: this.open = false;
73: }
74:
75: @Override
76: public void first() {
77: throw new UnsupportedOperationException(
78: "Returning to the first row is not supported by this result set.");
79: }
80:
81: @Override
82: public void previous() {
83: throw new UnsupportedOperationException("Going back is not supported by this result set.");
84: }
85:
86: @Override
87: public void relative(int rows) throws OntoDriverException {
88: setRowIndex(index + rows);
89: }
90:
91: @Override
92: public void last() throws OntoDriverException {
93: ensureOpen();
94: while (hasNext()) {
95: next();
96: }
97: }
98:
99: @Override
100: public void next() throws OntoDriverException {
101: ensureOpen();
102: if (!hasNext()) {
103: throw new NoSuchElementException();
104: }
105: index++;
106: }
107:
108: @Override
109: public void setRowIndex(int rowIndex) throws OntoDriverException {
110: ensureOpen();
111: if (rowIndex < index) {
112: throw new UnsupportedOperationException(
113: "Going back in this result set is not supported.");
114: }
115: if (rowIndex == index) {
116: return;
117: }
118: while (index <= rowIndex) {
119: next();
120: }
121: }
122: }